/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package render;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import utils.Reflection;

/**
 *
 * @author Canh
 */
public class CTCTableModel extends DefaultTableModel {

    List data;
    String[] header;
    String[] valuesName;

    public CTCTableModel() {
        data = new ArrayList();
        header = new String[0];
        valuesName = new String[0];
    }

    public String[] getHeader() {
        return header;
    }

    public void setHeader(String[] header) {
        this.header = header;
        reloadData();
    }

    public String[] getValuesName() {
        return valuesName;
    }

    public void setValuesName(String[] valuesName) {
        this.valuesName = valuesName;
        reloadData();
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
        reloadData();
    }

    void reloadData() {
        this.columnIdentifiers.clear();
        this.dataVector.clear();

        for (int i = 0; i < header.length; i++) {
            addColumn(header[i]);
        }

        for (int i = 0; i < data.size(); i++) {
            this.addRow(readValues(data.get(i)));
        }
    }

    public Object[] readValues(Object obj) {
        Object[] objs = new Object[valuesName.length];
        for (int i = 0; i < objs.length; i++) {
            objs[i] = Reflection.readMethodValue(obj, valuesName[i]);
        }
        return objs;
    }

   
}
